/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package Mainframe;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.*;

import Settings.*;
import GuiTools.*;

/**
 * Dialog to set the parameters for the Translate class invoked in LookUpPanel.
 * 
 * @author Christoph Schnack
 * @since Tribuit 1.1.2
 */
public class LookUpOptions{
	JRadioButton startWithRB,exactRB,containsRB,containsCharRB,endsWithRB,anagramsRB,resemblesRB;
	Vector<JRadioButton> buttons = new Vector<JRadioButton>();
	JCheckBox crawlMetasCB,regardCaseCB, considerPreFixCB;
	boolean startWith=true,exact,contains,containsChar,endsWith,anagrams,resembles;
	Settings SET;
	Language LANG;
	JDialog dialog;
	JPanel centerPanel,buttonPanel;
	Mainframe MAINFRAME;
	JButton closeButton,cancelButton;
	ButtonGroup bg;

	public LookUpOptions(Settings set,Mainframe mf){
		SET=set;
		MAINFRAME=mf;
		LANG=set.getLanguage();
		bg = new ButtonGroup();
    	startWithRB = new JRadioButton(LANG.getStartsWith(),true);
    	exactRB = new JRadioButton(LANG.getExactMatch());
    	containsRB = new JRadioButton(LANG.getContains());
    	endsWithRB = new JRadioButton(LANG.getEndsWith());
    	containsCharRB = new JRadioButton(LANG.getContainsChars());
    	anagramsRB = new JRadioButton(LANG.getAnagrams());
    	resemblesRB = new JRadioButton(LANG.getResembles());
    	bg.add(startWithRB);
    	bg.add(endsWithRB);
    	bg.add(containsRB);
    	bg.add(containsCharRB);
    	bg.add(exactRB);
    	bg.add(anagramsRB);
    	bg.add(resemblesRB);
    	
    	buttons.add(startWithRB);
    	buttons.add(endsWithRB);
    	buttons.add(exactRB);
    	buttons.add(resemblesRB);
    	buttons.add(containsRB);
    	buttons.add(containsCharRB);
    	buttons.add(anagramsRB);
    	crawlMetasCB = new JCheckBox(LANG.getCrawlMetas(),true);
    	considerPreFixCB = new JCheckBox(LANG.getConsiderPreFix(),false);
    	regardCaseCB = new JCheckBox(LANG.getCaseSensitivity(),false);
    	
    	JPanel rbPanel = new JPanel();
    	rbPanel.setBorder(BorderFactory.createTitledBorder(LANG.getSearchMethod()+":"));
    	rbPanel.setLayout(new BoxLayout(rbPanel,BoxLayout.Y_AXIS));
    	JLabel searchTermLabel = new JLabel(LANG.getSearchTerm());
    	searchTermLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,13));
    	JLabel inputLabel = new JLabel(LANG.getLUOptionInput());
    	inputLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,13));
    	rbPanel.add(searchTermLabel);
    	rbPanel.add(startWithRB);
    	rbPanel.add(endsWithRB);
    	rbPanel.add(exactRB);
    	rbPanel.add(resemblesRB);
    	rbPanel.add(containsRB);
    	rbPanel.add(containsCharRB);
    	rbPanel.add(anagramsRB);
    	rbPanel.add(inputLabel);
    	
    	JPanel cbPanel = new JPanel();
    	cbPanel.setBorder(BorderFactory.createTitledBorder(LANG.getMoreOptions()+":"));
    	cbPanel.setLayout(new BoxLayout(cbPanel,BoxLayout.Y_AXIS));
    	cbPanel.add(crawlMetasCB);
    	cbPanel.add(regardCaseCB);
    	cbPanel.add(considerPreFixCB);
    	
    	centerPanel = new JPanel();
    	centerPanel.setLayout(new GridLayout(1,2));
    	centerPanel.add(rbPanel);
    	centerPanel.add(cbPanel);
    	
    	closeButton = new JButton(LANG.getAccept());
    	closeButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			startWith=startWithRB.isSelected();
    			endsWith=endsWithRB.isSelected();
    			contains=containsRB.isSelected();
    			containsChar=containsCharRB.isSelected();
    			exact=exactRB.isSelected();
    			anagrams=anagramsRB.isSelected();
    			resembles=resemblesRB.isSelected();
    			dialog.dispose();
    		}
    	});
    	cancelButton = new JButton(LANG.getCancel());
    	cancelButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			dialog.dispose();
    		}
    	});
    	buttonPanel = new JPanel();
    	buttonPanel.add(closeButton);
    	buttonPanel.add(cancelButton);
    	setShortCuts();
    	
	}
	
	public void showUp(Language lang){
		LANG=lang;
		dialog = new JDialog(MAINFRAME,true);
		dialog.setLocationRelativeTo(MAINFRAME);
		dialog.setIconImage(GuiTools.getTribuitIcon());
		dialog.setTitle(LANG.getLookupParameters());
		startWithRB.setSelected(startWith);
		endsWithRB.setSelected(endsWith);
		containsRB.setSelected(contains);
		containsCharRB.setSelected(containsChar);
		anagramsRB.setSelected(anagrams);
		exactRB.setSelected(exact);
		resemblesRB.setSelected(resembles);
    	dialog.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getLookupParameters()));
    	dialog.getContentPane().add(BorderLayout.CENTER,centerPanel);
    	dialog.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
    	dialog.pack();
    	if(endsWith) endsWithRB.requestFocus();
		if(containsChar) containsCharRB.requestFocus();
		if(contains) containsRB.requestFocus();
		if(exact) exactRB.requestFocus();
		if(anagrams) anagramsRB.requestFocus();
		if(resembles) resemblesRB.requestFocus();
    	dialog.setVisible(true);
	}
	
	public String getSearchMethod(){
		String opt = "startsWith";
		if(endsWith) opt = "endsWith";
		if(containsChar) opt = "containsChars";
		if(contains) opt = "containsString";
		if(exact) opt = "exactMatch";
		if(anagrams) opt = "anagrams";
		if(resembles) opt = "resembles";
		return opt;
	}
	
	public boolean getConsiderCase(){
		return regardCaseCB.isSelected();
	}
	
	public boolean getConsiderPreFix(){
		return considerPreFixCB.isSelected();
	}
	
	public boolean getCrawlMetas(){
		return crawlMetasCB.isSelected();
	}
	
	public void setShortCuts(){
		InputMap inputMap = centerPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		ActionMap actionMap = centerPanel.getActionMap();
		inputMap.put(KeyStroke.getKeyStroke("ctrl ENTER"), "okAction");
		actionMap.put("okAction", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				closeButton.doClick();
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("ctrl D"), "cancelAction");
		actionMap.put("cancelAction", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				cancelButton.doClick();
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("UP"), "upAction");
		actionMap.put("upAction", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				for(int i=0;i<buttons.size();i++){
					if(buttons.elementAt(i).isSelected() && i>0){
						buttons.elementAt(i-1).doClick();
						break;
					}
				}
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("DOWN"), "downAction");
		actionMap.put("downAction", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				for(int i=0;i<buttons.size();i++){
					if(buttons.elementAt(i).isSelected() && i<buttons.size()-1){
						buttons.elementAt(i+1).doClick();
						break;
					}
				}
			}
		});
	}
}
